﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Pitaya_IDE
{
    public class AllImage
    {
        private Bitmap bmp;

        //普通文件图标
        public static Image DefaultMode1;
        public static Image DefaultMode2;
        public static Image DefaultMode3;
        public static Image DefaultMode4;
        public static Image DefaultMode5;

        //python图标
        public static Image PythonMode1;
        public static Image PythonMode2;
        public static Image PythonMode3;
        public static Image PythonMode4;
        public static Image PythonMode5;

        //c图标
        public static Image CMode1;
        public static Image CMode2;
        public static Image CMode3;
        public static Image CMode4;
        public static Image CMode5;

        //sh图标
        public static Image ShMode1;
        public static Image ShMode2;
        public static Image ShMode3;
        public static Image ShMode4;
        public static Image ShMode5;

        //文件标题栏关闭按钮
        public static Image CloseMode1;
        public static Image CloseMode2;
        public static Image CloseMode3;
        public static Image CloseMode4;
        public static Image CloseMode5;


        public static Image MinMode1;
        public static Image MinMode2;
        public static Image MinMode3;

        public static Image MaxMode1;
        public static Image MaxMode2;
        public static Image MaxMode3;

        public static Image MaxMode4;
        public static Image MaxMode5;
        public static Image MaxMode6;

        public static Image ExitMode1;
        public static Image ExitMode2;
        public static Image ExitMode3;

        public static Image[] FImage_file;

        public static Image[] ImgLft = new Image[8];
        public static Image[] ImgLftB = new Image[2];

        public static Image[] Refh_Search = new Image[2];

        Color backcolor;

        public AllImage(Color _backcolor)
        {
            backcolor = _backcolor;

            bmp = new Bitmap("de.bmp");

            DefaultMode1 = MixedGraph(Color.FromArgb(1, 4, 9), bmp);
            DefaultMode2 = MixedGraph(Color.FromArgb(9, 12, 16), bmp);
            DefaultMode3 = MixedGraph(Color.FromArgb(13, 17, 23), bmp);
            DefaultMode4 = MixedGraph(Color.FromArgb(22, 27, 34), bmp);
            DefaultMode5 = MixedGraph(Color.FromArgb(45, 49, 57), bmp);


            bmp = new Bitmap("del.bmp");

            CloseMode1 = MixedGraph(Color.FromArgb(1, 4, 9), bmp, 0);
            CloseMode2 = MixedGraph(Color.FromArgb(13, 17, 23), bmp, 1);
            CloseMode3 = MixedGraph(Color.FromArgb(13, 17, 23), bmp, 2);
            CloseMode4 = MixedGraph(Color.FromArgb(13, 17, 23), bmp, 3);
            CloseMode5 = MixedGraph(Color.FromArgb(13, 17, 23), bmp, 4);

            
            bmp = new Bitmap("Ctrl.bmp");

            MinMode1        = MixedGraph(Color.FromArgb(1, 4, 9)    , bmp, (uint)0);
            MinMode2     = MixedGraph(Color.FromArgb(13, 17, 23) , bmp, (uint)0);
            MinMode3     = MixedGraph(Color.FromArgb(38, 41, 47) , bmp, (uint)0);

            MaxMode1        = MixedGraph(Color.FromArgb(1, 4, 9)    , bmp, (uint)1);
            MaxMode2     = MixedGraph(Color.FromArgb(13, 17, 23) , bmp, (uint)1);
            MaxMode3     = MixedGraph(Color.FromArgb(38, 41, 47) , bmp, (uint)1);

            MaxMode4       = MixedGraph(Color.FromArgb(1, 4, 9)    , bmp, (uint)2);
            MaxMode5    = MixedGraph(Color.FromArgb(13, 17, 23) , bmp, (uint)2);
            MaxMode6    = MixedGraph(Color.FromArgb(38, 41, 47) , bmp, (uint)2);

            ExitMode1      = MixedGraph(Color.FromArgb(1, 4, 9)    , bmp, (uint)3);
            ExitMode2   = MixedGraph(Color.FromArgb(13, 17, 23) , bmp, (uint)3);
            ExitMode3  = MixedGraph(Color.FromArgb(210, 17, 34), bmp, (uint)3);

            bmp = new Bitmap("file.bmp");

            FImage_file = new Image[((bmp.Width / 38) - 1) * 2];

            FImage_file[0] = FileImage(backcolor, bmp, false, 1);
            FImage_file[1] = FileImage(backcolor, bmp, true, 1);

            FImage_file[2] = FileImage(backcolor, bmp, false, 2);
            FImage_file[3] = FileImage(backcolor, bmp, true, 2);

            FImage_file[4] = FileImage(backcolor, bmp, false, 3);
            FImage_file[5] = FileImage(backcolor, bmp, true, 3);

            FImage_file[6] = FileImage(backcolor, bmp, false, 4);
            FImage_file[7] = FileImage(backcolor, bmp, true, 4);

            FImage_file[8] = FileImage(backcolor, bmp, false, 5);
            FImage_file[9] = FileImage(backcolor, bmp, true, 5);

            FImage_file[10] = FileImage(backcolor, bmp, false, 6);
            FImage_file[11] = FileImage(backcolor, bmp, true, 6);

            FImage_file[12] = FileImage(backcolor, bmp, false, 7);
            FImage_file[13] = FileImage(backcolor, bmp, true, 7);

            FImage_file[14] = FileImage(backcolor, bmp, false, 8);
            FImage_file[15] = FileImage(backcolor, bmp, true, 8);

            Refh_Search[0] = FileImage(backcolor, bmp, false, 3);
            Refh_Search[1] = FileImage(backcolor, bmp, true, 3);

            bmp = new Bitmap("de.bmp");
        }


        public static void SetColor(Color color)
        {
            Bitmap bmp;

            bmp = new Bitmap("left1.bmp");
            ImgLft[0] = AllImage.LeftMixedGraph(1, bmp, color);
            ImgLft[1] = AllImage.LeftMixedGraph(0.6, bmp, color);
            bmp = new Bitmap("left2.bmp");
            ImgLft[2] = AllImage.LeftMixedGraph(1, bmp, color);
            ImgLft[3] = AllImage.LeftMixedGraph(0.6, bmp, color);
            bmp = new Bitmap("left3.bmp");
            ImgLft[4] = AllImage.LeftMixedGraph(1, bmp, color);
            ImgLft[5] = AllImage.LeftMixedGraph(0.6, bmp, color);
            bmp = new Bitmap("left4.bmp");
            ImgLft[6] = AllImage.LeftMixedGraph(1, bmp, color);
            ImgLft[7] = AllImage.LeftMixedGraph(0.6, bmp, color);


            bmp = new Bitmap("L1.bmp");
            ImgLftB[0] = AllImage.LeftMixedGraph(1, bmp, color);
            ImgLftB[1] = AllImage.LeftMixedGraph(0.6, bmp, color);

        }

        public static Image GetImage(string str, int R)
        {
            if (str.Equals("py"))
            {
                if (R == 1) return PythonMode1;
                else if (R == 9) return PythonMode2;
                else if (R == 13) return PythonMode3;
                else if (R == 22) return PythonMode4;
                else if (R == 45) return PythonMode5;
            }
            else if (str.Equals("c"))
            {
                if (R == 1) return CMode1;
                else if (R == 9) return CMode2;
                else if (R == 13) return CMode3;
                else if (R == 22) return CMode4;
                else if (R == 45) return CMode5;
            }
            else if (str.Equals("sh"))
            {
                if (R == 1) return ShMode1;
                else if (R == 9) return ShMode2;
                else if (R == 13) return ShMode3;
                else if (R == 22) return ShMode4;
                else if (R == 45) return ShMode5;
            }
            else if (str.Equals("default"))
            {
                if (R == 1) return DefaultMode1;
                else if (R == 9) return DefaultMode2;
                else if (R == 13) return DefaultMode3;
                else if (R == 22) return DefaultMode4;
                else if (R == 45) return DefaultMode5;
            }
            
            return DefaultMode1;
        }

        public static Image GetFolderIco(bool unfold, Size size,Color color)
        {
            Bitmap bmp;
            Graphics g;
            Pen pen;

            bmp = new Bitmap(size.Width, size.Height);
            g = Graphics.FromImage(bmp);
            pen = new Pen(color);
            //pen = new Pen(Color.FromArgb(50,50,50));
            for (int i = 0; i < g.DpiX; i++)
            {
                g.DrawLine(pen, i, 0, i, g.DpiY);
            }

            Pen[] pen1 = new Pen[4];
            pen1[0] = new Pen(Color.FromArgb(140, 83, 26));
            pen1[1] = new Pen(Color.FromArgb(197,197,179));
            pen1[2] = new Pen(Color.FromArgb( 90,148,179));
            pen1[3] = new Pen(Color.FromArgb(  1,  4, 51));

            if (unfold)
            {
                float tmpAX, tmpAY, tmpBX, tmpBY;
                tmpAX = ((float)size.Width / 12 * 8);
                tmpAY = ((float)size.Height / 2);
                tmpBX = ((float)size.Width / 12 * 6);
                tmpBY = ((float)size.Height / 6 * 4);
                g.DrawLine(pen1[0], tmpAY, tmpAX - 0, tmpBY, tmpBX - 0);
                g.DrawLine(pen1[1], tmpAY, tmpAX - 1, tmpBY, tmpBX - 1);
                g.DrawLine(pen1[2], tmpAY, tmpAX - 2, tmpBY, tmpBX - 2);
                g.DrawLine(pen1[3], tmpAY, tmpAX - 3, tmpBY, tmpBX - 3);
                tmpBY = ((float)size.Height / 6 * 2);
                g.DrawLine(pen1[0], tmpAY, tmpAX - 0, tmpBY, tmpBX - 0);
                g.DrawLine(pen1[1], tmpAY, tmpAX - 1, tmpBY, tmpBX - 1);
                g.DrawLine(pen1[2], tmpAY, tmpAX - 2, tmpBY, tmpBX - 2);
                g.DrawLine(pen1[3], tmpAY, tmpAX - 3, tmpBY, tmpBX - 3);

                g.DrawLine(pen, 0, tmpBX - 2, size.Height, tmpBX - 2);
                g.DrawLine(pen, 0, tmpBX - 3, size.Height, tmpBX - 3);
            }
            else
            {
                float tmpAX, tmpAY, tmpBX, tmpBY;
                tmpAX = ((float)size.Width / 12 * 8);
                tmpAY = ((float)size.Height / 2);
                tmpBX = ((float)size.Width / 12 * 6);
                tmpBY = ((float)size.Height / 6 * 4);
                g.DrawLine(pen1[0], tmpAX - 0, tmpAY, tmpBX - 0, tmpBY);
                g.DrawLine(pen1[1], tmpAX - 1, tmpAY, tmpBX - 1, tmpBY);
                g.DrawLine(pen1[2], tmpAX - 2, tmpAY, tmpBX - 2, tmpBY);
                g.DrawLine(pen1[3], tmpAX - 3, tmpAY, tmpBX - 3, tmpBY);
                tmpBY = ((float)size.Height / 6 * 2);
                g.DrawLine(pen1[0], tmpAX - 0, tmpAY, tmpBX - 0, tmpBY);
                g.DrawLine(pen1[1], tmpAX - 1, tmpAY, tmpBX - 1, tmpBY);
                g.DrawLine(pen1[2], tmpAX - 2, tmpAY, tmpBX - 2, tmpBY);
                g.DrawLine(pen1[3], tmpAX - 3, tmpAY, tmpBX - 3, tmpBY);

                g.DrawLine(pen, tmpBX - 2, 0, tmpBX - 2, size.Height);
                g.DrawLine(pen, tmpBX - 3, 0, tmpBX - 3, size.Height);
            }

            return bmp;
        }

        public static Image GetFileIco(string str,Color color)
        {
            if (str.Equals("py") || str.Equals("Py") || str.Equals("PY") || str.Equals("pY"))
            {
                return MixedGraph(color, new Bitmap("py.bmp"));
            }
            else if (   str.Equals("c") || str.Equals("C") || 
                        str.Equals("h") || str.Equals("H") || 
                        str.Equals("cpp") || str.Equals("CPP"))
            {
                return MixedGraph(color, new Bitmap("c.bmp"));
            }
            else if (str.Equals("sh") || str.Equals("SH"))
            {
                return MixedGraph(color, new Bitmap("sh.bmp"));
            }
            else if (str.Equals("txt") || str.Equals("TXT"))
            {
                return MixedGraph(color, new Bitmap("txt.bmp"));
            }
            else if (str.Equals("pdf") || str.Equals("PDF"))
            {
                return MixedGraphWhite(color, new Bitmap("pdf.bmp"));
            }
            else if (   str.Equals("bmp") || str.Equals("BMP") ||
                        str.Equals("png") || str.Equals("PNG") ||
                        str.Equals("jpg") || str.Equals("JPG") ||
                        str.Equals("jpeg")|| str.Equals("JPEG") )
            {
                return MixedGraphWhite(color, new Bitmap("pic.bmp"));
            }
            else if (   str.Equals("zip") || str.Equals("ZIP") ||
                        str.Equals("rar") || str.Equals("RAR") ||
                        str.Equals("7z")  || str.Equals("7Z")  )
            {
                return MixedGraphWhite(color, new Bitmap("compress.bmp"));
            }
            else
            {
                return MixedGraph(color, new Bitmap("de.bmp"));
            }
        }

        public static Image LeftMixedGraph(double Proportion, Bitmap bmp, Color backcolor)
        {
            Bitmap bmpout = new Bitmap(bmp.Width, bmp.Height);
            Graphics g = Graphics.FromImage(bmpout);
            Pen pen = new Pen(Color.FromArgb(0,0,0)); //混合后的颜色
            double Y;

            for (int y = 0; y < bmp.Height; y++)
            {
                for (int x = 0; x < bmp.Width; x++)
                {
                    Color tmp = bmp.GetPixel(x, y);
                    Y = (0.3 * tmp.R + 0.6 * tmp.G + 0.1 * tmp.B) / 256;
                    if (Y > 0.2)
                    {
                        pen.Color = Color.FromArgb((int)(tmp.R * Proportion),
                                                   (int)(tmp.G * Proportion),
                                                   (int)(tmp.B * Proportion));
                    }
                    else
                    {
                        pen.Color = backcolor;
                    }

                    g.DrawEllipse(pen, x, y, 1, 1);
                }
            }
            return bmpout;
        }

        private static Image MixedGraphWhite(Color color, Bitmap bmp)
        {
            Bitmap bmpout = new Bitmap(bmp.Width, bmp.Height);
            Graphics g = Graphics.FromImage(bmpout);
            Pen pen = new Pen(color); //混合后的颜色
            double Y;

            for (int y = 0; y < bmp.Height; y++)
            {
                for (int x = 0; x < bmp.Width; x++)
                {
                    Color tmp1 = bmp.GetPixel(x, y);
                    Color tmp = Color.FromArgb(255 - tmp1.R, 255 - tmp1.G, 255 - tmp1.B);
                    Y = (0.3 * tmp.R + 0.4 * tmp.G + 0.3 * tmp.B) / 256;
                    if (Y < 0.05)
                    {
                        pen.Color = Color.FromArgb((int)(color.R * (1 - Y) + tmp.R * Y),
                                                    (int)(color.G * (1 - Y) + tmp.G * Y),
                                                    (int)(color.B * (1 - Y) + tmp.B * Y));
                    }
                    else
                    {
                        pen.Color = tmp;
                    }

                    g.DrawEllipse(pen, x, y, 1, 1);
                }
            }
            return bmpout;
        }
        private static Image MixedGraph(Color color, Bitmap bmp) 
        {
            Bitmap bmpout = new Bitmap(bmp.Width, bmp.Height);
            Graphics g = Graphics.FromImage(bmpout);
            Pen pen = new Pen(color); //混合后的颜色
            double Y;

            for (int y = 0; y < bmp.Height; y++)
            {
                for (int x = 0; x < bmp.Width; x++)
                {
                    Color tmp = bmp.GetPixel(x, y);
                    Y = (0.3 * tmp.R + 0.6 * tmp.G + 0.1 * tmp.B) / 256;
                    if (Y < 0.4)
                    {
                        pen.Color =  Color.FromArgb((int)(color.R * (1 - Y) + tmp.R * Y),
                                                    (int)(color.G * (1 - Y) + tmp.G * Y),
                                                    (int)(color.B * (1 - Y) + tmp.B * Y));
                    }
                    else
                    {
                        pen.Color = tmp;
                    }
                    
                    g.DrawEllipse(pen, x, y, 1, 1);
                }
            }
            return bmpout;
        }

        private Image MixedGraph(Color backcolor, Bitmap bmp, int mode)
        {
            Bitmap bmpout = new Bitmap(bmp.Width / 2, bmp.Height);
            Graphics g = Graphics.FromImage(bmpout);
            Pen pen = new Pen(Color.Black); //混合后的颜色
            double Y,Y2;
            Color SelecColor = Color.FromArgb(128, 128, 128);

            for (int y = 0; y < bmp.Height; y++)
            {
                for (int x = 0; x < bmp.Width / 2; x++)
                {
                    pen.Color = backcolor;

                    if (mode == 2 || mode == 4)  //降低亮度
                    {
                        Color tmp = bmp.GetPixel(x, y);
                        Y = (0.3 * tmp.R + 0.6 * tmp.G + 0.1 * tmp.B) / 256;
                        if (Y > 0.2)
                        {
                            pen.Color = Color.FromArgb(255, (int)(tmp.R * 0.6),
                                                       (int)(tmp.G * 0.6),
                                                       (int)(tmp.B * 0.6));
                        }
                    }
                    else if (mode == 1 || mode == 3)
                    {
                        Color tmp = bmp.GetPixel(x, y);
                        Y = (0.3 * tmp.R + 0.6 * tmp.G + 0.1 * tmp.B) / 256;
                        if (Y > 0.2)
                        {
                            pen.Color = tmp;
                        }
                    }

                    if (mode == 1 || mode == 2)  //选中
                    {
                        Color tmp = bmp.GetPixel(x + bmp.Width / 2, y);
                        Color tmp2 = pen.Color;

                        Y2 = (0.3 * tmp2.R + 0.6 * tmp2.G + 0.1 * tmp2.B) / 256;
                        Y = (0.3 * tmp.R + 0.6 * tmp.G + 0.1 * tmp.B) / 256;

                        if (Y > 0.078 && Y2 < 0.1)
                        {
                            pen.Color = Color.FromArgb((int)(tmp2.R * (1 - Y) + SelecColor.R * Y),
                                                        (int)(tmp2.G * (1 - Y) + SelecColor.G * Y),
                                                        (int)(tmp2.B * (1 - Y) + SelecColor.B * Y));
                        }
                        else if (Y2 < 0.1)
                        {
                            pen.Color = backcolor;
                        }
                    }

                    g.DrawEllipse(pen, x, y, 1, 1);
                }
            }
            return bmpout;
        }

        private Image MixedGraph(Color color, Bitmap bmp , Color color2)
        {
            Bitmap bmpout = new Bitmap(bmp.Width, bmp.Height);
            Graphics g = Graphics.FromImage(bmpout);
            Pen pen = new Pen(color); //混合后的颜色
            double Y;

            for (int y = 0; y < bmp.Height; y++)
            {
                for (int x = 0; x < bmp.Width; x++)
                {
                    Color tmp = bmp.GetPixel(x, y);
                    Y = (0.3 * tmp.R + 0.6 * tmp.G + 0.1 * tmp.B) / 256;

                    pen.Color = Color.FromArgb((int)(color.R * (1 - Y) + color2.R * Y),
                                               (int)(color.G * (1 - Y) + color2.G * Y),
                                               (int)(color.B * (1 - Y) + color2.B * Y));

                    g.DrawEllipse(pen, x, y, 1, 1);
                }
            }
            return bmpout;
        }

        private Image MixedGraph(Color backcolor, Bitmap bmp, uint mode)
        {
            Bitmap bmpout = new Bitmap(bmp.Width / 4, bmp.Height);
            Graphics g = Graphics.FromImage(bmpout);
            Pen pen = new Pen(backcolor); //混合后的颜色
            double Y;

            for (int y = 0; y < bmp.Height; y++)
            {
                for (int x = 0; x < bmp.Width/4; x++)
                {
                    Color tmp = bmp.GetPixel(x + (int)mode * 79, y);
                    Y = (0.3 * tmp.R + 0.6 * tmp.G + 0.1 * tmp.B) / 256;

                    if (backcolor.R == 210) tmp = Color.FromArgb(255, 255, 255);

                    if (Y < 0.2)
                    {
                        pen.Color = Color.FromArgb((int)(backcolor.R * (1 - Y) + tmp.R * Y),
                                                    (int)(backcolor.G * (1 - Y) + tmp.G * Y),
                                                    (int)(backcolor.B * (1 - Y) + tmp.B * Y));
                    }
                    else
                    {
                        pen.Color = tmp;
                    }

                    g.DrawEllipse(pen, x, y, 1, 1);
                }
            }

            return bmpout;
        }

        private Image FileImage(Color backcolor,Bitmap bmp,bool bright,int cnt)
        {
            Bitmap bmpout = new Bitmap(bmp.Height, bmp.Height);
            Graphics g = Graphics.FromImage(bmpout);
            Pen pen = new Pen(backcolor); //混合后的颜色
            double Y;

            for (int y = 0; y < bmpout.Height; y++)
            {
                for (int x = 0; x < bmpout.Width ; x++)
                {
                    if (bright)
                    {
                        pen.Color = bmp.GetPixel(x, y);
                    }
                    else
                    {
                        pen.Color = backcolor;
                    }

                    Color tmp = bmp.GetPixel(x + cnt * 38, y);
                    Y = (0.3 * tmp.R + 0.6 * tmp.G + 0.1 * tmp.B) / 256;

                    if (Y < 0.2)
                    {
                        pen.Color = Color.FromArgb((int)(pen.Color.R * (1 - Y) + tmp.R * Y),
                                                    (int)(pen.Color.G * (1 - Y) + tmp.G * Y),
                                                    (int)(pen.Color.B * (1 - Y) + tmp.B * Y));
                    }
                    else
                    {
                        pen.Color = tmp;
                    }

                    

                    g.DrawEllipse(pen, x, y, 1, 1);
                }
            }

            return bmpout;
        }
    

    }
}